package org.ling.xuan.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.ling.xuan.dialog.FindFileInEditDialog;
import org.ling.xuan.util.LoggerUtil;

import java.util.List;

public class LxBatchOpen  extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        FindFileInEditDialog dialog = new FindFileInEditDialog(e.getProject(), true);
        Project project = e.getProject();
        if(project==null){
            return;
        }
        if(dialog.showAndGet()){
            List<String> list = dialog.getFileList();
            if(list!=null&&list.size()>0){
                    ApplicationManager.getApplication().invokeLater(() -> {
                        FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
                        LocalFileSystem localFileSystem = LocalFileSystem.getInstance();
                        for (String filePath : list) {
                            LoggerUtil.log(project,"打开文件:"+filePath);
                            VirtualFile virtualFile = localFileSystem.findFileByPath(filePath);
                            if (virtualFile != null) {
                                fileEditorManager.openFile(virtualFile, true);
                            }
                        }
                    });
            }
        }
    }
}
